package com.syg.boot.core.autoconfigure;

import com.syg.boot.util.time.DateUtil;
import com.syg.common.context.SygEnvConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author syg
 * @date 2021年11月25日17:34:39
 * @version 1.0
 */
@Configuration
public class YmlSourcesLoadConfiguration {
    private static final Logger log = LoggerFactory.getLogger(YmlSourcesLoadConfiguration.class);

    @Bean
    public PropertySourcesPlaceholderConfigurer properties(ConfigurableEnvironment environment) {
        System.setProperty("logging.path", "F:/test" + DateUtil.getCurTM() + "/logs");
        String active = environment.getProperty("spring.profiles.active");
        String applicationName = environment.getProperty("spring.application.name");
        // syg.all.namespace 在哪个配置文件中配置
        String namespace = environment.getProperty("syg.all.namespace");
        SygEnvConstants.put(SygEnvConstants.APPLICATION_NAME_KEY, applicationName);
        SygEnvConstants.put(SygEnvConstants.PROFILES_ACTIVE_KEY, active);
        SygEnvConstants.put(SygEnvConstants.SYG_ALL_NAMESPACE, namespace);
        MutablePropertySources propertySources = environment.getPropertySources();
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        String pathObj = environment.getProperty("zsf.boot.core.ymlResourcePath", String.class, null);
        // 加在其他的配置文件
        if (pathObj != null) {
            String[] paths = pathObj.split(",");
            for (String path : paths) {
                this.load(propertySources, path);
                String[] array = path.split("[.]");
                String envPath = array[0] + "-" + active + "." + array[1];
                this.load(propertySources, envPath);
            }
        }

        boolean globalEnabled = environment.getProperty("zsf.boot.bizdata.global.enabled", Boolean.class, false);
        boolean cacheEnabled = environment.getProperty("zsf.boot.bizdata.cache.enabled", Boolean.class, false);
        if (globalEnabled || cacheEnabled) {
            String globalBizdataPath = "config/zsf/bizdata/config/global-bizdata-" + active + ".yml";
            this.load(propertySources, globalBizdataPath);
            log.info("^_^加载全局业务数据源，" + globalBizdataPath + "^_^");
        }

        configurer.setPropertySources(propertySources);
        // 这个对象是new出来的,为什么可以返回？难道可以有多个yml文件的对象嘛？
        return configurer;
    }

    private void load(MutablePropertySources propertySources, String path) {
        YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
        List envList = new ArrayList();

        try {
            log.info("^_^加载自定义YML文件，当前文件路径:" + path + "^_^");
            envList = loader.load(path, new ClassPathResource(path));
        } catch (IOException var6) {
            log.error("path:{}不存在请确认！", path);
        } catch (Throwable var7) {
            log.error("path:{}不存在请确认！！", path);
        }

        if (!StringUtils.isEmpty((List)envList)) {
            propertySources.addLast((PropertySource)((List)envList).get(0));
        }

    }
}
